export default {
    meetsTel(tel) {
        if (tel === "") {
            return '请输入手机号';
        }
        let onoff = /^1[3456789]\d{9}$/.test(tel);
        if (onoff) {
            return true;
        }
        return '请输入正确格式的手机号'

    },
    meetsCode(code) {
        if (code === "") {
            return null;
        }
        return /^[0-9a-zA-Z]{6}$/.test(code);
    },
    meetsPass(pass) {
        if (pass === '') {
            return null;
        }
        return /^(?=.*[a-zA-Z])(?=.*\d)[^]{8,}$/.test(pass);
    },
    meetsAccount(a) {
        if (a.trim() === '') {
            return null;
        }
        return /([a-zA-Z0-9_@.]{6,20})+$/.test(a);
    },
    meetsWx(pass) {
        if (pass === "") {
            return null;
        }
        return /([-_a-zA-Z0-9]{6,20})+$/.test(pass);
    },
    meetsQQ(pass) {
        if (pass === "") {
            return null;
        }
        return /^[1-9]\d{4,20}$/.test(pass);
    },
    meetsEmpty(str, length = 1) {
        str = str.replace(/^\s*|\s*$/g, "");
        if (str === "") {
            //空
            return null;
        }
        if (str.length >= length) {
            return true;
        } else {
            return false;
        }
    },
    meetsID(id) {
        let reg =
            /^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|31)|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}([0-9]|x|X)$/;
        if (id === "") {
            return null;
        }
        return reg.test(id);
    },
    // 设置多少年之后的时间
    setYearTime(time, year, type = "-") {
        var d1 = new Date(time);
        var d2 = new Date(d1);
        d2.setFullYear(d2.getFullYear() + year);
        d2.setDate(d2.getDate() - 1);
        let data = new Date(d2);
        let Y = data.getFullYear();
        let M = data.getMonth() + 1;
        M < 10 ? (M = "0" + M) : M;
        let D = data.getDate();
        D < 10 ? (D = "0" + D) : D;
        let h = data.getHours();
        h < 10 ? (h = "0" + h) : h;
        let m = data.getMinutes();
        m < 10 ? (m = "0" + m) : m;
        let s = data.getSeconds();
        s < 10 ? (s = "0" + s) : s;
        return `${Y}${type}${M}${type}${D} ${h}:${m}:${s}`;
    },
    // 判断两个时间之间的时间间隔多少天
    meetsTwoTime(first, two) {
        let t = new Date(first).getTime() - new Date(two).getTime();
        t = t / 1000 / 60 / 60 / 24;
        if (t > 0 && t < 1) {
            t = 1;
        } else {
            t = parseInt(t);
        }

        return t;
    },
    // 判断2个时间相差几个月
    meetsTwoMonth(time, del = 12) {
        let oldTime = new Date(time);
        let oldY = oldTime.getFullYear();
        let oldM = oldTime.getMonth();
        let oldD = oldTime.getDate();
        let oldTimes = new Date(time).getTime();

        let nowTime = new Date();
        let nowY = nowTime.getFullYear();
        let nowM = nowTime.getMonth();
        let nowD = nowTime.getDate();
        let nowTimes = nowTime.getTime();

        if (oldTimes <= nowTimes) {
            //过期
            return -1;
        }
        let difY = oldY - nowY;
        let difM = oldM - nowM;
        let difD = oldD - nowD;
        let months = difY * 12 + difM;
        if (months >= del && difD > 0) {
            return -2; //大于预订时间
        }
        return months <= 0 ? 0 : difD > 0 ? months : months - 1; //剩余时间
    },
    meetsSpace(name) {
        name = name.replace(/\s+/g, "");
        return name;
    },
    meetsMd5(pass) {
        pass = md5(pass);
        return pass;
    },
    // 保留两位小数
    changeTwo(e, len = 2) {
        let val = e;
        val = val.replace(/[^0-9.]/gi, "");
        if (len == 0) {
            val = val.replace(/[^0-9]/gi, "");
        }

        let arr = val.split(".");
        if (arr.length >= 3) {
            val = arr[0] + "." + arr[1];
        }
        arr = val.split(".");
        if (arr[1]) {
            // 小数位存在，保留2位
            arr[1] = arr[1].slice(0, len);
            val = arr[0] + "." + arr[1];
        }

        return val;
    },
    // 保留两位小数
    changeOne(e, len = 2) {
        let val = e;
        val = val.replace(/[^0-9.]/gi, "");
        if (len == 0) {
            val = val.replace(/[^0-9]/gi, "");
        }

        let arr = val.split(".");
        if (arr.length >= 3) {
            val = arr[0] + "." + arr[1];
        }
        arr = val.split(".");
        if (arr[1]) {
            // 小数位存在，保留1位
            arr[1] = arr[1].slice(0, 1);
            val = arr[0] + "." + arr[1];
        }

        return val;
    },

    // 加0
    add0(num) {
        let n = Number(num);
        n < 10 ? (n = "0" + n) : (n = n);
        return n;
    },
    setStr(str, len = 10) {
        str = '' + str;
        if (str.length > len) {
            str = str.slice(0, len) + '...'
        }
        ;
        return str;
    },
    setIcon(icon) {
        if (icon) {
            icon = icon.split(',')
            return icon[0];
        }
        return ''
    },
    //
    setArr(str) {
        let arr = [];
        if (str) {
            arr = str.split(',');
        }
        return arr;
    },
    // 获取cid
    getCid() {
        let pinf = plus.push.getClientInfo();
        let cid = pinf.clientid; //客户端标识
        return cid;
    },
    setTime(time = new Date()) {
        let date = new Date(time);
        let Y = date.getFullYear();
        let M = date.getMonth() + 1;
        M = M < 10 ? '0' + M : M;
        let D = date.getDate();
        D = D < 10 ? '0' + D : D;
        let h = date.getHours();
        h = h < 10 ? '0' + h : h;
        let m = date.getMinutes();
        m = m < 10 ? '0' + m : m;
        let s = date.getSeconds();
        s = s < 10 ? '0' + s : s;
        return `${Y}-${M}-${D} ${h}:${m}:${s}`
    },
};
